package no.ntnu.idi.deid.decision.validators.findPhrase.helpers;

import java.util.ArrayList;
import java.util.List;

import no.ntnu.idi.deid.documentunits.Phrase;
import no.ntnu.idi.deid.documentunits.Sentence;

public class LevenshteinWordDistance {

		public static Phrase findPhrase(Sentence sentence, String phrase, int distance) {
			String[] phraseArray = phrase.split(" ");
			int numberOfMatches = 0;
			Phrase returnPhrase = null;
			List<Integer> indexList = new ArrayList<Integer>();
			if(phraseArray != null) {
				for(int j = 0; j < phraseArray.length; j++) {
					for(int i = 0; i < sentence.size(); i++) {	
						if(phraseArray[j].toLowerCase().equals(sentence.get(i).getStripped())) { 
							numberOfMatches++;
							indexList.add(i);
						}
					}
				}
			}
			if(phraseArray.length <= 2 ) {
				System.err.println("Fare for at LevhensteinDistanceWord matcher alt for mange ord! Frasen består av for få. Burde brukt normalisert søk");
			}
			
			if(phraseArray.length <= (numberOfMatches+distance)) {
				returnPhrase = new Phrase();
				for(Integer index: indexList) {
					returnPhrase.add(sentence.get(index));
				}
			}
			return returnPhrase;
		}
		
}
